//
//  KxBzV.swift
//  Auscultation
//
//  Created by 来 on 2020/12/21.
//

import Foundation
 
class KxBzV: UIView {
    
    var kxBzBj: KxBzBjV!
    
    var canEdit: Bool = true
    
    var bzColor: UIColor! {
        didSet {
            backgroundColor = bzColor.withAlphaComponent(0.2)
        }
    }
    
    var callRect: ((KxBzV, CGRect, Bool) -> Void)!
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setBcolor(self.bzColor)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        self.isUserInteractionEnabled = true
        self.backgroundColor = UIColor.init(lgf_HexString: "FF4D4D")?.withAlphaComponent(0.2)
        self.bzColor = UIColor.init(lgf_HexString: "FF4D4D")
//        self.lgf_AddLong(target: self, action: #selector(longTap), duration: 0.2)
        self.lgf_AddTap(target: self, action: #selector(longTap))
    }
    
    func addPanView() {
        if self.lgf_ViewName == "已标注" {
            return
        }
        let topLeft = KxBzPanV.init()
        topLeft.type = 1
        addSubview(topLeft)
        topLeft.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
            make.height.equalToSuperview().multipliedBy(0.5)
        }
        let topRight = KxBzPanV.init()
        topRight.type = 2
        addSubview(topRight)
        topRight.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.right.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
            make.height.equalToSuperview().multipliedBy(0.5)
        }
        
        let bottomLeft = KxBzPanV.init()
        bottomLeft.type = 3
        addSubview(bottomLeft)
        bottomLeft.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
            make.height.equalToSuperview().multipliedBy(0.5)
        }
        
        let bottomRight = KxBzPanV.init()
        bottomRight.type = 4
        addSubview(bottomRight)
        bottomRight.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.right.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
            make.height.equalToSuperview().multipliedBy(0.5)
        }
        
        topLeft.callPan = { [weak self] (point) in
            DispatchQueue.main.async {
                let l_left = max(0, point.x)
                let l_top = max(0, point.y)
                let top = self!.superview!.lgf_Height - 20 - (self!.superview!.lgf_Height - self!.lgf_Height - self!.lgf_Y)
                let left = self!.superview!.lgf_Width - 20 - (self!.superview!.lgf_Width - self!.lgf_X - self!.lgf_Width)
                self?.snp.updateConstraints { (make) in
                    make.left.equalTo(min(left, l_left))
                    make.top.equalTo(min(top, l_top))
                }
            }
        }
        topRight.callPan = { [weak self] (point) in
            DispatchQueue.main.async {
                let l_right = min(0, point.x)
                let l_top = max(0, point.y)
                let top = self!.superview!.lgf_Height - 20 - (self!.superview!.lgf_Height - self!.lgf_Height - self!.lgf_Y)
                let right = -(self!.superview!.lgf_Width - 20 - self!.lgf_X)
                self?.snp.updateConstraints { (make) in
                    make.top.equalTo(min(top, l_top))
                    make.right.equalTo(max(right, l_right))
                }
            }
        }
        bottomLeft.callPan = { [weak self] (point) in
            DispatchQueue.main.async {
                let l_bottom = min(0, point.y)
                let l_left = max(0, point.x)
                let bottom = -(self!.superview!.lgf_Height - 20 - self!.lgf_Y)
                let left = self!.superview!.lgf_Width - 20 - (self!.superview!.lgf_Width - self!.lgf_X - self!.lgf_Width)
                self?.snp.updateConstraints { (make) in
                    make.bottom.equalTo(max(bottom, l_bottom))
                    make.left.equalTo(min(left, l_left))
                }
            }
        }
        bottomRight.callPan = { [weak self] (point) in
            DispatchQueue.main.async {
                let l_right = min(0, point.x)
                let l_bottom = min(0, point.y)
                let right = -(self!.superview!.lgf_Width - 20 - self!.lgf_X)
                let bottom = -(self!.superview!.lgf_Height - 20 - self!.lgf_Y)
                self?.snp.updateConstraints { (make) in
                    make.bottom.equalTo(max(bottom, l_bottom))
                    make.right.equalTo(max(right, l_right))
                }
            }
        }
    }
    
    deinit {
        debugPrint("标注框释放了")
    }
    
    func setBcolor(_ bcolor: UIColor!) {
        if bcolor != nil {
            self.layer.sublayers?.forEach({ (l) in
                if l.lgf_Name == "虚线框" {
                    l.removeFromSuperlayer()
                }
            })
            let path = UIBezierPath.init(roundedRect: layer.bounds, cornerRadius: 0)
            let slayer = CAShapeLayer.init()
            slayer.lgf_Name = "虚线框"
            slayer.path = path.cgPath
            slayer.lineWidth = 2.0
            slayer.lineDashPattern = [4, 4]
            slayer.fillColor = UIColor.clear.cgColor
            slayer.strokeColor = bcolor.cgColor
            self.layer.addSublayer(slayer)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func longTap() {
        if kxBzBj == nil {
            let oldColor = self.bzColor
            self.bzColor = UIColor.white
            backgroundColor = UIColor.black.withAlphaComponent(0.8)
            setBcolor(self.bzColor)
            if canEdit {
                kxBzBj = KxBzBjV()
                kxBzBj.showIn(UIApplication.shared.keyWindow?.rootViewController?.view ?? self, self.lgf_ViewName == "已标注" ? "查看" : "标注") { [weak self] (kxBzBj, name, isEdit) in
                    self?.kxBzBj = nil
                    self?.bzColor = oldColor
                    self?.setBcolor(self?.bzColor)
                    if isEdit != 0 {
                        self?.callRect?(self!, self?.frame ?? CGRect.zero, isEdit == 2)
                    }
                }
            } else {
                UIView.animate(withDuration: 1.0) {
                    self.bzColor = oldColor
                    self.setBcolor(self.bzColor)
                }
                self.callRect(self, self.frame, true)
            }
        }
    }
    
}

class KxBzPanV: UIView {
    
    var type: Int = 0
    
    var callPan: ((CGPoint) -> Void)!
    
    var startPoint: CGPoint!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        self.isUserInteractionEnabled = true
        self.lgf_AddPan(target: self, action:  #selector(pan(_:)))
    }
    
    @objc func pan(_ sender: UIPanGestureRecognizer) {
        let translation  = sender.translation(in: self.superview)
        if sender.state == .began {
            if type == 1 {
                startPoint = CGPoint.init(x: self.superview!.lgf_X, y: self.superview!.lgf_Y)
            } else if type == 2 {
                startPoint = CGPoint.init(x: -(self.superview!.superview!.lgf_Width - self.superview!.lgf_X - self.superview!.lgf_Width), y: self.superview!.lgf_Y)
            } else if type == 3 {
                startPoint = CGPoint.init(x: self.superview!.lgf_X, y: -(self.superview!.superview!.lgf_Height - self.superview!.lgf_Y - self.superview!.lgf_Height))
            } else if type == 4 {
                startPoint = CGPoint.init(x: -(self.superview!.superview!.lgf_Width - self.superview!.lgf_X - self.superview!.lgf_Width), y: -(self.superview!.superview!.lgf_Height - self.superview!.lgf_Y - self.superview!.lgf_Height))
            }
        } else if sender.state == .changed {
            callPan?(CGPoint.init(x: startPoint.x + translation.x, y: startPoint.y + translation.y))
            debugPrint(CGPoint.init(x: startPoint.x + translation.x, y: startPoint.y + translation.y))
        } else if sender.state == .ended {
            
        }
        
        sender.setTranslation(translation, in: self.superview)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
